# -*- coding: utf-8 -*-
'''
Created on Apr 17, 2012

@author: LONG HOANG GIANG
'''

import sys, os
sys.path.append(os.path.expanduser('/home5/vietcntt/longhoanggiang/python'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib import Http, commonlib
from urlparse import urljoin
import gzip
import simplejson as json
import datetime
import re
from lxml import etree


def getChapter(name, url):
    print 'getChapter url = {0}'.format(url)
    images = []
    if url.startswith('http://vechai.info'):
        tree = Http.getXMLTree(url)
        for item in tree.xpath("//*[@id='vcfix']/a"):
            link = item.get('href', '')
            if link == '': continue
            if re.search('http://i.imgur.com/[a-zA-Z0-9]+', link): link += '.jpg'
            images.append(link)
    elif re.search('http://vc1.vechai.info/\d+/\d+', url):
        tree = Http.getXMLTree(url)
        for item in tree.xpath("//*[@id='vcfix']//img"):
            link = item.get('src', '')
            if link == '': continue
            if re.search('http://i.imgur.com/[a-zA-Z0-9]+', link): link += '.jpg'
            images.append(link)
    print '------------------------------------'
    print 'NAME: {0}'.format(name)
    for img in images:
        print img
    print '------------------------------------'
    return {'chapter': name, 'images': images}


def getListLink(output):
    url = 'http://vechai.info/Doremon-che/'
    tree = Http.getXMLTree(url)
    data = []
    for item in tree.xpath("//strong[contains(., 'READ ONLINE')]/../../following-sibling::*[name()='a']"):
        link = item.get('href', '')
        if link != '':
            link = urljoin(url, link)
            name = commonlib.stringify(item)
            item = getChapter(name, link)
            if len(item['images']) > 0: data.append(item)
    if not os.path.exists(os.path.dirname(output)): os.makedirs(os.path.dirname(output), 0777)
    fp = gzip.open(output, 'wb')
    jsondata = json.dumps(data)
    fp.write(jsondata)
    fp.close()

if __name__ == '__main__':
    
    getListLink("/home5/vietcntt/public_html/site-api-vietcntt/res/truyen/doremon-che/output.gz")
    
    print '>> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)